<?php

/**
 * Created by Atom.
 * User: qiliping
 * Date: 2017/1/18
 * Time: 11:18
 */
class Offline_order_model extends CI_Model
{

  /**
   * 变更面授课订单为已付款状态
   * @param  [type] $order_id [description]
   * @return [type]           [description]
   */
  public function change_pay_success($order_id)
  {
    $this->db->trans_start();

    //查询订单
    $sql = "SELECT * FROM {$this->db->dbprefix}shopping_orders where order_id = {$order_id} LIMIT 1";
    $order = $this->db->query($sql)->row_array();
    if(empty($order)) return 404;
    if($order['order_status']!=0) return 406;

    //查询student
    $student_sql = "SELECT * FROM {$this->db->dbprefix}o2o_student WHERE user_id = {$order['buyer_user_id']} LIMIT 1";
    $student = $this->db->query($student_sql)->row_array();

    //1.修改订单状态
    $order_status = array(
      'order_status' => 1, 
      'modify_on' => time(),
      );

    $this->db->update('shopping_orders',$order_status, array('order_id' => $order_id ));

    //2.添加订单记录
    $history = array(
      'order_id' => $order_id,
      'comment' => '管理员设置为已付款',
      'order_status' => 1,
      'create_on' => time(),
      'create_by' => 'school_admin',
    );
    $this->db->insert('shopping_order_history',$history);

    if($student){
      //3.插入学生课程表edu_o2o_student_course
      $order_item_sql = "SELECT * FROM {$this->db->dbprefix}shopping_order_item WHERE order_id={$order_id}";
      $order_items = $this->db->query($order_item_sql)->result_array();

      foreach ($order_items as $oitem) {
        $stu_cour = array(
          'user_id' => $student['user_id'],
          'student_id' => $student['student_id'],
          'course_id' => $oitem['course_id'],
          'course_type' => $oitem['course_type'],
          'school_id' => $order['school_id'],
          'school_campus_id' => $order['school_id'],
          'effective_time' => $oitem['effective_time'],
          'order_num' => $order['order_num'],
          'status' => 0,
          'add_time' => time(),
        );
        $this->db->insert("o2o_student_course",$stu_cour);
      }
    }
    
    $this->db->trans_complete();

    return 1;
  }

  /**
  **  获取订单信息
  */
  public function get_order_by_id($order_id)
  {
    $sql = "SELECT * FROM {$this->db->dbprefix}shopping_orders where order_id = {$order_id} LIMIT 1";
    return $this->db->query($sql)->row_array();
  }

  /**
  **  查询学校订单
  */
  public function get_offline_order_list_by_school($limit = 0, $offset = 10, $data = array())
  {
    $where = " 1=1 ";
    if(!empty($data['school_id'])){
      $where .= " AND ord.school_id = {$data['school_id']} ";
    }
    if(!empty($data['order_type'])){
      $where .= " AND ord.order_type = {$data['order_type']} ";
    }
    if($data['status'] != 'all'){
      $where .= " AND ord.order_status = {$data['status']} ";
    }
    if(!empty($data['order_num'])){
      $where .= " AND ord.order_num = {$data['order_num']} ";
    }
    if(!empty($data['school_campus_id'])){
      $where .= " AND ord.school_campus_id = {$data['school_campus_id']} ";
    }
    if(!empty($data['st'])){
      $data['st'] = strtotime($data['st']);
      $where .= " AND ord.order_time > {$data['st']} ";
    }
    if(!empty($data['et'])){
      $data['et'] = strtotime($data['et']) + 86400;
      $where .= " AND ord.order_time < {$data['et']} ";
    }
    if(!empty($data['mobile'])){
      $where .= " AND u.mobile = {$data['mobile']} ";
    }

    $this->db->trans_start();
    $sql = "SELECT SQL_CALC_FOUND_ROWS ord.*,cam.name as campus_name,stu.student_id,stu.name as student_name,u.mobile FROM {$this->db->dbprefix}shopping_orders ord ";
    $sql .= " left join {$this->db->dbprefix}o2o_school_campus cam on cam.id = ord.school_campus_id ";
    $sql .= " left join {$this->db->dbprefix}o2o_student stu on stu.user_id = ord.buyer_user_id ";
    $sql .= " left join {$this->db->dbprefix}user u on u.id=ord.buyer_user_id ";
    $sql .= " where $where LIMIT $limit, $offset ";
    $result = $this->db->query($sql)->result_array();
    $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();

    $this->db->trans_complete();

    return array("total_count" => $row_num['row_num'], "result" => $result);
  }


}
